<?php
class EventsController extends AppController {

	var $name = 'Events';
	var $permissions = array(
					'add' => array('admin','manag','users'),
					'index' => '*',
					'view' => '*',
					'edit' => array('admin','manag','users'),
					'delete' => array('admin','manag','users'),
					'join' => array('admin','manag','users'),
					'active' => array('admin','manag','users')
	);
	function index() {
		$this->Event->recursive = 1;
		$events = $this->paginate();		
		$my_events = $this->Event->find('all',array('conditions'=>array('Event.user_id'=>$this->Auth->user('id')),'limit'=>'4'));
		$this->loadModel('Friend');
		$my_friends = $this->Friend->find('all',array('conditions'=>array('Friend.user_id'=>$this->Auth->user('id')),'limit'=>'8','order'=>array('Friend.created DESC')));
		
		$this->set('user_id',$this->Auth->user('id'));
		$this->set(compact('events','my_events','my_friends'));
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid event', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->Event->recursive = 2;
		$event = $this->Event->read(null, $id);
// 		debug($event);
		$this->set('event', $event);
		$this->set('user_id',$this->Auth->user('id'));
	}

	function add($user_id = null) {
		if (!empty($this->data)) {
// 			$this->data['Event']['created'] = date('Y-m-d H:i:s');
			$this->Event->create();
			if ($this->Event->save($this->data)) {
				$this->Session->setFlash(__('The event has been saved', true));
				$this->redirect($this->referer());
			} else {
				$this->Session->setFlash(__('The event could not be saved. Please, try again.', true));
			}
			$user_id = $this->data['User']['id'];
		}
		$yachtCharters_ = $this->Event->VidYachtCharter->find('all',array('conditions'=>array('VidYachtCharter.user_id'=>$user_id)));
		foreach ($yachtCharters_ as $key => $v) {
			$yachtCharters[$v['VidYachtCharter']['id']] = $v['VidYachtCharter']['name'].' ( '.$v['VidYachtCharter']['date_from'].' : '.$v['VidYachtCharter']['date_to'].' )';
		}
		$this->set(compact('yachtCharters','user_id'));
	}
	
	function join($event_id=null){
// 		$this->Event->EventsJoin->create();
		$this->loadModel('EventsJoin');
		$this->data['EventsJoin']['event_id'] = $event_id;
		$this->data['EventsJoin']['user_id'] = $this->Auth->user('id');
		if ($this->EventsJoin->save($this->data)) {
			$this->Session->setFlash(__('The joind event has been saved', true));			
		} else {
			$this->Session->setFlash(__('The joind event could not be saved. Please, try again.', true));
		}
		$this->redirect($this->referer());
	}

	function active($id= null){
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid Event', true));
			$this->redirect($this->referer());
		}
		
		$this->data = $this->Event->read(null, $id);
		$this->data['Event']['status'] = !$this->data['Event']['status'];
		if (!empty($this->data)) {
			if ($this->Event->save($this->data)) {
				$this->Session->setFlash(__('The event has been saved', true));
			} else {
				$this->Session->setFlash(__('The event could not be saved. Please, try again.', true));
			}
		}
		$this->redirect($this->referer());		
	}
	
	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid event', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Event->save($this->data)) {
				$this->Session->setFlash(__('The event has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The event could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Event->read(null, $id);
		}
		$yachtCharters = $this->Event->YachtCharter->find('list');
		$users = $this->Event->User->find('list');
		$posts = $this->Event->Post->find('list');
		$uploads = $this->Event->Upload->find('list');
		$this->set(compact('yachtCharters', 'users', 'posts', 'uploads'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for event', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Event->delete($id)) {
			$this->Session->setFlash(__('Event deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Event was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
